/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.csvgenerator.engine.vg;

import com.csvgenerator.engine.value.ValueProducer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class SetValueGenerator extends ValueGenerator {

    private NumberValueGenerator sizeSampler = null;
    private NumberValueGenerator indexSampler = null;
    private List<ValueProducer> producerReferences = new ArrayList<ValueProducer>();
    private String prefix = "";
    private String infix = "";
    private String suffix = "";
    private boolean uniqueValues = false;
    private List<String> fixedValues = new ArrayList<String>();
    private List<String> allValues = new ArrayList<String>();
    private List<String> currentValues = new ArrayList<String>();

    @Override
    public String nextValue() {
        //clear previous initialization
        allValues.clear();
        currentValues.clear();

        //add all current values of all producers
        for (ValueProducer vp : producerReferences) {
            allValues.add(vp.getCurrentValue());
        }

        //add all fixed values
        allValues.addAll(fixedValues);

        long setIndex = 0;
        String nextValue = prefix;

        long setSize = Long.parseLong(sizeSampler.getCurrentValue());
        String temp;
        long i = setSize;

        while (i > 0) {
            setIndex = Long.parseLong(indexSampler.getCurrentValue());
            temp = allValues.get(Integer.parseInt("" + setIndex));
            if (uniqueValues) {
                while (currentValues.contains(temp)) {
                    setIndex = Long.parseLong(indexSampler.getCurrentValue());
                    temp = allValues.get(Integer.parseInt("" + setIndex));
                }
                currentValues.add(temp);
            }
            nextValue += allValues.get(Integer.parseInt("" + setIndex)) + infix;
            i--;
        }
        if (setSize > 0) {
            nextValue = nextValue.substring(0, nextValue.lastIndexOf(infix)) + suffix;
        }
        currentValue = nextValue;
        return nextValue;
    }

    /**
     * @return the sizeSampler
     */
    public NumberValueGenerator getSizeSampler() {
        return sizeSampler;
    }

    /**
     * @param sizeSampler the sizeSampler to set
     */
    public void setSizeSampler(NumberValueGenerator sizeSampler) {
        this.sizeSampler = sizeSampler;
    }

    /**
     * @return the indexSampler
     */
    public NumberValueGenerator getIndexSampler() {
        return indexSampler;
    }

    /**
     * @param indexSampler the indexSampler to set
     */
    public void setIndexSampler(NumberValueGenerator indexSampler) {
        this.indexSampler = indexSampler;
    }

    /**
     * @return the producerReferences
     */
    public List<ValueProducer> getProducerReferences() {
        return producerReferences;
    }

    /**
     * @param producerReferences the producerReferences to set
     */
    public void setProducerReferences(List<ValueProducer> producerReferences) {
        this.producerReferences = producerReferences;
    }

    /**
     * @return the prefix
     */
    public String getPrefix() {
        return prefix;
    }

    /**
     * @param prefix the prefix to set
     */
    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    /**
     * @return the infix
     */
    public String getInfix() {
        return infix;
    }

    /**
     * @param infix the infix to set
     */
    public void setInfix(String infix) {
        this.infix = infix;
    }

    /**
     * @return the suffix
     */
    public String getSuffix() {
        return suffix;
    }

    /**
     * @param suffix the suffix to set
     */
    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    /**
     * @return the uniqueValues
     */
    public boolean isUniqueValues() {
        return uniqueValues;
    }

    /**
     * @param uniqueValues the uniqueValues to set
     */
    public void setUniqueValues(boolean uniqueValues) {
        this.uniqueValues = uniqueValues;
    }

    /**
     * @return the fixedValues
     */
    public List<String> getFixedValues() {
        return fixedValues;
    }

    /**
     * @param fixedValues the fixedValues to set
     */
    public void setFixedValues(List<String> fixedValues) {
        this.fixedValues = fixedValues;
    }
}
